package com.toivan.mt.fragment;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.shizhefei.fragment.LazyFragment;
import com.toivan.mt.R;
import com.toivan.mt.adapter.MtExpressionAdapter;
import com.toivan.mt.model.MtExpression;
import com.toivan.mt.utils.MtCallback;
import com.toivan.mt.utils.MtJsonParser;
import com.toivan.sdk.MtSDK;

import java.util.ArrayList;
import java.util.List;

public class MtExpressionFragment extends LazyFragment {

    private List<MtExpression> list = new ArrayList<>();
    private MtExpressionAdapter adapter;



    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);

        setContentView(R.layout.fragment_sticker_recyclerview);

        if (getContext() == null) return;

        list.clear();
//        list.add(MtExpression.NO_EXPRESSION);
        List<MtExpression> expressions = MtJsonParser.getInstance().getAllExpressions();
        if (expressions == null || expressions.size() == 0) {
            MtJsonParser.getInstance().getExpressions( new MtCallback<List<MtExpression>>() {
                @Override
                public void callback(List<MtExpression> bean) {
                    if (bean != null) {
                        list.addAll(bean);
                        showExpression();
                    }
                }
            });
        } else {
            list.addAll(expressions);
            showExpression();
        }
    }

    private void showExpression() {
        RecyclerView expressionRV = (RecyclerView) findViewById(R.id.recyclerView);
        adapter = new MtExpressionAdapter(list);
        expressionRV.setLayoutManager(new GridLayoutManager(getContext(), 5));
        expressionRV.setAdapter(adapter);
    }

    public void cancelExpression() {
        if (adapter != null) {
            adapter.cancelSelected();
        }
    }



}


